<?php
// +----------------------------------------------------------------------
// | 齐力众信
// +----------------------------------------------------------------------
// | htto://www.qlzhx.com
// +----------------------------------------------------------------------
// | Author: 苏明煌  < qq23025079@126.com >
// +----------------------------------------------------------------------


namespace Addons\DataTime;
use Common\Controller\Addon;

/**
 * 时间插件
 * @time 2014-09-19
 */
 
 class DataTimeAddon extends Addon {
 	
	# 信息
	public $info = array(
		'name'=>'DataTime',
		'title'=>'时间插件',
		'description'=>'方便选择时间',
		'status'=>1,
		'author'=>'qlzhx',
		'version'=>'0.1'
	);
	
	
	// 判断是否加载过
	static private $is_load = 0;
	
	/**
	 * 用于显示时间插件
	 * @param array $data
	 * @使用方式 array( 'name', 'value', array( 'attribute'=>'attribute_value'[,...] ) );
	 * @param string $show
	 */
	public function showTime( $data, $show = array('Y-m-d', 'yyyy-mm-dd') )
	{
		/*
		 * 一个页面调用多次，只加载一次js
		 */
		$load = ++ self::$is_load ;
		
		list( $name, $value, $attributeArray ) = $data;
		
		list( $one, $two ) = $show;

		$value = empty( $value ) ? $_SERVER['REQUEST_TIME'] : $value;

		$value = date( $one , $value );
		
		$attribute = '';
		
		foreach( $attributeArray as $attrKey=>$attrValue){
			$attribute .= $attrKey . '="' . $attrValue .'" ';
		}
		
		$this->assign( compact( 'name', 'value', 'attribute', 'load', 'two' ) );
		
		$this->display('showTime');

		// echo 1;
	}
	
	# 安装
	public function install()
	{
		$prefix = C("DB_PREFIX");
		$model = D();
       	$tableH = "INSERT INTO `{$prefix}hooks` (`name`, `description`, `type`, `update_time`, `addons`) VALUES( 'showTime', '时间插件调用钩子', 1, ". NOW_TIME .", 'DataTime')";
		$model->execute($tableH);
		return true;
		
	}
	
	
	# 卸载
	public function uninstall()
	{
		$prefix = C("DB_PREFIX");	
		
		$model = D();
        $model->execute("delete from {$prefix}hooks where name='showTime'");
		
		return true;
	}
 }
 
 
